<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row>
          <el-col :span="21">
            <el-form :inline="true" :model="formData" class="demo-form-inline">
             <el-form-item label="关键字">
               <el-input v-model.trim="formData.keyword" placeholder="根据文章标题搜索" size="small"></el-input>
             </el-form-item>
             <el-form-item label="状态" style="padding-left:33px">
               <el-select v-model="formData.state" placeholder="请选择" size="small">
                 <el-option label="启用"  :value="1"></el-option>
                 <el-option label="禁用" :value="0"></el-option>
               </el-select>
             </el-form-item>
             <el-form-item>
               <el-button size="small" @click="clearForm">清除</el-button>
               <el-button type="primary" size="small" @click="search">搜索</el-button>
             </el-form-item>
           </el-form>
          </el-col>
          <el-col :span="3" style="padding-left:44px">
            <el-button type="success" icon="el-icon-edit" size="small" @click="handleAdd">新增技巧</el-button>
          </el-col>
        </el-row>
        <el-alert
          type="info"
          :closable="false"
          show-icon>
          <template #title>
            <span>数据一共{{counts}}条</span>
          </template>
        </el-alert>
        <el-table :data="tableData" style="width: 100%;" :header-cell-style="{background: '#fafafa'}">
          <el-table-column type="index" width="80" label="序号" > </el-table-column>
          <el-table-column label="文章标题" prop="title" width="340">
            <template #default="{row}">
               <span>{{row.title}}</span>
               <a href="#" v-if="row.videoURL" @click.prevent="handleClick(row.videoURL)">
                 <i class="el-icon-film"></i>
               </a>
            </template>
         </el-table-column>
          <el-table-column label="阅读数" prop="visits"> </el-table-column>
          <el-table-column label="录入人" prop="username"> </el-table-column>
          <el-table-column label="录入时间" prop="createTime"> </el-table-column>
          <el-table-column label="状态" prop="state"> </el-table-column>
          <el-table-column label="操作" width="170">
            <template #default="{row}">
              <el-button type="text" @click="handlePreview(row)">预览</el-button>
              <el-button type="text" @click="editState(row)" >{{row.state==='已启用'? '禁用':'启用' }}</el-button>
              <el-button type="text"  :disabled="row.state==='已启用'? true:false" @click="handleEdit(row)">修改</el-button>
              <el-button type="text" :disabled="row.state==='已启用'? true:false" @click="del(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          style="text-align:right;padding-top:27px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="10"
          layout="  prev, pager, next,sizes,jumper"
          :total="counts"
          v-if="counts>=pagesize"
          >
        </el-pagination>
        <!-- 播放视频的对话框 -->
        <div class="box" v-if="showDialog"  >
          <div class="top">
          <div class="icon" @click="showDialog=false">X</div>
          <video :src="src" controls autoplay muted ref="video" ></video>
          </div>
        </div >
        <AddArticles :showAdd.sync="showAdd" @reload='getTableList' ref="add" ></AddArticles>
        <!-- 预览文章对话框 -->
        <ArticlesPreview :showPreview.sync="showPreview" ref="preview" :row="row"></ArticlesPreview>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import AddArticles from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'
import dayjs from 'dayjs'
export default {
  name: 'articles',
  components: {
    AddArticles,
    ArticlesPreview
  },
  data () {
    return {
      formData: { // 表单数据
        state: '',
        keyword: ''
      },
      tableData: [], // 表格数据
      page: 1,
      pagesize: 10,
      counts: 0,
      showDialog: false, // 播放视频的弹框
      showAdd: false, // 添加文章的弹层出现
      showPreview: false,
      row: {}, // 点击预览时当前这一行的信息
      src: ''
    }
  },
  created () {
    this.getTableList()
  },
  methods: {
    // 发请求获取文章列表数据
    async getTableList () {
      const { data: { counts, items } } = await list({ page: this.page, pagesize: this.pagesize })
      this.counts = counts
      this.tableData = this.formList(items)
    },
    // 处理表格数据的函数
    formList (items) {
      const arr = []
      items.forEach(item => {
        if (item.createTime) {
          item.createTime = dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss')
        }
        if (item.state === 1) {
          item.state = '已启用'
        } else {
          item.state = '已禁用'
        }
        arr.push(item)
      })
      return arr
    },
    // 页面条数发生变化执行的函数
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.getTableList()
    },
    // 页面页码发生变化执行的函数
    handleCurrentChange (val) {
      this.page = val
      this.getTableList()
    },
    // 启用按钮执行的事件
    async editState (row) {
      row.state === '已启用' ? row.state = '已禁用' : row.state = '已启用'
      const temp = row.state === '已启用' ? 1 : 0
      await changeState({ id: row.id, state: temp })
      await this.getTableList()
      this.$message.success('操作成功')
    },
    // 删除按钮
    del (row) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message.success('删除成功!')
        this.getTableList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 预览文章的功能
    handlePreview (row) {
      this.showPreview = true
      this.row = row
      this.$refs.preview.getPreviewList(row)
    },
    // 播放视频的功能
    handleClick (src) {
      this.showDialog = true
      this.src = src
    },
    // 添加文章的功能
    handleAdd () {
      this.showAdd = true
      this.$refs.add.title = '新增文章'
      this.page = 1
    },
    // 修改文章的功能
    handleEdit (row) {
      this.showAdd = true
      this.$refs.add.title = '修改文章'
      this.$refs.add.getDetailArticle(row)
    },
    // 清除表单数据
    clearForm () {
      // console.log(11)
      this.formData = {
        title: '',
        state: ''
      }
      this.getTableList()
    },
    // 搜索框功能
    async search () {
      this.page = 1
      const { data: { items, counts } } = await list({ ...this.formData, page: this.page, pagesize: this.pagesize })
      this.tableData = this.formList(items)
      this.counts = counts
    }
  }
}
</script>

<style scoped lang="scss">
.el-table__row i {
  color: #00f;
}
.box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: rgba(0,0,0,.3);
  z-index: 9999;
  .top  {
    position: relative;
    width: 100%;
    height: 100%;
    .icon {
    width: 40px;
    height: 40px;
    color: #fff;
    text-align: center;
    line-height: 40px;
    font-size: 20px;
    background-color:rgba(0,0,0,0.4);
    border-radius: 50px;
    position: absolute;
    top: 10%;
    left: 50%;
    transform: translate(-50%,-50%);
    z-index: 10;
    }
    video {
    width: 600px;
    height: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    z-index: 10;
    }

  }
}
.app-container {
  padding: 10px;
}

</style>
